package com.lqian.base;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class InetAddress_and_InetsocketAddress {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress ia = InetAddress.getLocalHost();
        System.out.println(InetAddress.getLocalHost());// luoqiandeMacBook-Pro.local/127.0.0.1
        System.out.println(InetAddress.getByName("127.0.0.1"));// /127.0.0.1
        //www.baidu.com/183.232.231.172
        System.out.println(InetAddress.getByName("www.baidu.com"));
        System.out.println(ia.getHostName());// luoqiandeMacBook-Pro.local

        //======================================
        InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8080);
        addr = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8080);//另一种构造方法
        String hostName = addr.getHostName();
        int port = addr.getPort();
        InetAddress address = addr.getAddress();
        String sname = address.getHostName();
        String saddress = address.getHostAddress();


        System.out.println(hostName);
        System.out.println(port);
        System.out.println(sname);
        System.out.println(saddress);


    }


}
